package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.Dto.DishDto;
import com.example.domain.DishFlavor;
import com.example.mapper.DishFlavorMapper;
import com.example.service.IDishFlavorService;
import com.example.service.ex.DeleteException;
import com.example.service.ex.InsertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 餐品味道业务，与餐品业务相关
 */
@Service
public class DishFlavorServiceImpl implements IDishFlavorService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Override
    public void save(DishFlavor dishFlavor) {
        //使用MP中的方法对菜品的口味介绍进行添加
        int insert = dishFlavorMapper.insert(dishFlavor);
        //判断添加是否成功不成功报错
        if (insert<=0){
            throw new InsertException("在添加口味是报未知的异常");
        }
    }
    //根据餐品id查询出需要回显的味道数据
    @Override
    public List<DishFlavor> list(Long id) {
        //创建添加构造器
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(id!=null,DishFlavor::getDishId,id);
        //查询数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(lambdaQueryWrapper);
        return dishFlavors;
    }

    @Override
    public void delete(DishDto dishDto) {
        //创建条件构造器
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(dishDto!=null,DishFlavor::getDishId,dishDto.getId());
        int delete = dishFlavorMapper.delete(lambdaQueryWrapper);
        if (delete<=0){
            throw new DeleteException("删除数据时产生未知的异常");
        }
    }
}
